package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ChangeWithdrawPasswordRequest;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import glideHelp.GlideHelper;
import utils.Utils;
/*
* 修改资金密码
* */

public class ChangeWithdrawPasswordActivity extends BaseActivity implements TextWatcher{

    @BindView(R.id.old_password)
    EditText old_password;
    @BindView(R.id.new_password)
    EditText new_password;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.authcode_register)
    EditText authcodeRegister;
    @BindView(R.id.auth_img_register)
    ImageView authImgRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_withdraw_password);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        old_password.addTextChangedListener(this);
        new_password.addTextChangedListener(this);
        GlideHelper.loadImageWithoutCache(this, UserInfo.getQuickDomain() + "captcha", authImgRegister);
    }

    @OnClick({R.id.auth_img_register, R.id.confirm})
    public void onViewClicked(View view) {
        removeSoftKeyboard(view);
        switch(view.getId()) {
            case R.id.auth_img_register:
                GlideHelper.loadImageWithoutCache(this, UserInfo.getQuickDomain() + "captcha", authImgRegister);
                break;
            case R.id.confirm:
                if (authcodeRegister.getText().length() == 0) {
                    Utils.showToast("请输入图形验证码");
                    return;
                }else if (new_password.getText().toString().equals(old_password.getText().toString())) {
                    Utils.showToast("新密码和旧密码不能相同");
                    return;
                }

                RetrofitBase.getRetrofit().create(ChangeWithdrawPasswordRequest.class).getResult(UserInfo.getToken(),
                        null,
                        new_password.getText().toString(),
                        new_password.getText().toString(),
                        old_password.getText().toString(),
                        authcodeRegister.getText().toString()
                        )
                        .compose(RxHelper.handleResult())
                        .subscribe(new RxSubscribe<Object>(this) {
                            @Override
                            protected void _onNext(Object modifyProfileResponse) {
                                finish();
                            }

                            @Override
                            protected boolean showDialog() {
                                return true;
                            }
                        });
            break;
        }
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(old_password.getText().length() == 6 && new_password.getText().length() == 6){
            confirm.setEnabled(true);
//            confirm.setBackgroundResource(R.drawable.bg_corners_yellow);
        } else {
            confirm.setEnabled(false);
//            confirm.setBackgroundResource(R.drawable.bg_corners_grey);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
}
